<?php
/**
 * Created by PhpStorm.
 * User: u
 * Date: 2019/3/26
 * Time: 10:11
 */

namespace common\logics\taskshop;

use Yii;
use common\widgets\Tool;
use yii\base\BaseObject;

class TaskShopLogic extends BaseObject implements ITaskShop
{
    /*** @var  \common\sources\read\taskcommod\TaskCommodAccess $RTaskCommodAccess**/
    private $RTaskCommodAccess;
    /** @var   \common\sources\write\taskcommod\TaskCommodAccess $WTaskCommodAccess ***/
    private $WTaskCommodAccess;
    /** @var   \common\sources\read\taskshopimage\TaskShopImageAccess $RTaskShopImageAccess **/
    private $RTaskShopImageAccess;
    /** @var  \common\sources\write\taskshopimage\TaskShopImageAccess $WTaskShopImageAccess ****/
    private $WTaskShopImageAccess;
    /** @var \common\sources\read\buyuser\BuyUserAccess $RBuyUserAccess **/
    private $RBuyUserAccess;
    private $valueservice;

    public function init()
    {
        $this->valueservice =  require dirname(dirname(dirname(dirname(__FILE__)))).'/config/value-service-config.php';
        $this->RTaskCommodAccess = Yii::$container->get("RTaskCommodAccess");
        $this->WTaskCommodAccess = Yii::$container->get("WTaskCommodAccess");
        $this->RTaskShopImageAccess = Yii::$container->get("RTaskShopImageAccess");
        $this->WTaskShopImageAccess = Yii::$container->get("WTaskShopImageAccess");
        $this->RBuyUserAccess = Yii::$container->get("RBuyUserAccess");
        parent::init(); // TODO: Change the autogenerated stub
    }

    public function addTaskShop($data)
    {
        if(empty($data['bus_id']))
        {
            return Tool::returnJson([],401,"商家id不能为空");
        }
        if(empty($data['task_commod_id']))
        {
            return Tool::returnJson([],402,"主任务id不能为空");
        }
        if(count($data['task_shop_info']) == 0)
        {
            return Tool::returnJson([],403,"图片任务不能为空");
        }
        if(empty($data['safetyCode']))
        {
            return Tool::returnJson([],405,"安全不能为空");
        }
        if(md5($data['bus_id'].$data['task_commod_id']) != $data['safetyCode'])
        {
            return Tool::returnJson([],406,"安全码不正确");
        }
        if(empty($data['praise_type']))
        {
            return Tool::returnJson([],413,'好评类型不能为空');
        }
        //最多五个任务
        if(count($data['task_shop_info'])>5)
        {
            return Tool::returnJson([],415,"任务数量过多,图文任务不得超过5个");
        }
        //商家用户
        $buyUser = $this->RBuyUserAccess->getBuyUserInfo($data['bus_id']);
        if(empty($buyUser)){
            return Tool::returnJson([],407,'商家用户不存在');
        }
        $task_commod = $this->RTaskCommodAccess->getTaskCommodById($data['task_commod_id']);
        if(empty($task_commod))
        {
            return Tool::returnJson([],408,'商家发布的主任务不存在');
        }
        if($task_commod['plat_type'] == 1 ||$task_commod['plat_type'] ==2)
        {
            if($task_commod['task_type'] != 2)
            {
                return Tool::returnJson([],409,'非图文任务不能进行该操作');
            }
            if($task_commod['task_num'] >5)
            {
                return Tool::returnJson([],410,'图文任务不能超过5单');
            }
        }
        //判断提交数量和商家任务数量是否对等
        if($task_commod['task_num'] != count($data['task_shop_info']))
        {
            return Tool::returnJson([],411,'发布任务数量和图文任务数量不匹配');
        }
        $list = [];
        $trr = [];
        foreach ($data['task_shop_info'] as &$task_shop)
        {
            if(empty($task_shop['slide_imgs']))
            {
                return Tool::returnJson([],410,'商品图片不能为空');
            }
            if($data['praise_type'] ==3 && empty($task_shop['comment']))
            {
                return Tool::returnJson([],411,'自定义好评评论不能为空');
            }
            foreach ($task_shop['slide_imgs'] as $key => $slides)
            {
                if($key == 0)
                {
                    $trr['slide_img1'] = $slides;
                }
                if($key == 1)
                {
                    $trr['slide_img2'] = $slides;
                }
                if($key == 2)
                {
                    $trr['slide_img3'] = $slides;
                }
                if($key == 3)
                {
                    $trr['slide_img4'] = $slides;
                }
                if($key == 4)
                {
                    $trr['slide_img5'] = $slides;
                }
            }
            $trr['praise_type'] = $data['praise_type'];
            $trr['size'] = isset($task_shop['size']) ? $task_shop['size'] : '';
            $trr['colour'] = $task_shop['colour'];
            $trr['comment_keyword'] = $data['comment_keyword'];
            $trr['task_commod_id'] = $data['task_commod_id'];
            $trr['bus_id'] = $data['bus_id'];
            $trr['comment'] = isset($task_shop['comment']) ? $task_shop['comment'] : '';
            $list[] = $trr;
            unset($trr);
        }
        $transaction = Yii::$app->db->beginTransaction();
        $res = false;
        try{
            $task_shop_image = $this->RTaskShopImageAccess->getTaskShop($data['task_commod_id']);
            if(count($task_shop_image) >0)
            {
                //如果已经有了 就删除
                $this->WTaskShopImageAccess->delTaskShop($data['task_commod_id']);
            }
            foreach ($list as $shops)
            {
                $res = $this->WTaskShopImageAccess->addTaskShop($shops);
            }
            if($res)
            {
                $parase_type_pepper = $this->valueservice['task_image_type'][$list[0]['praise_type']];
                $task_img_pepper = $this->valueservice['task_img_pepper'];
                //修改主任务状态 是否是 图文任务状态
                $this->WTaskCommodAccess->modifyTaskShopImage(['id'=>$data['task_commod_id'],"is_task_image"=>1,'img_praise_type' => $list[0]['praise_type'],
                    'parase_type_pepper' => $parase_type_pepper,'task_img_pepper' => $task_img_pepper]);
                $transaction->commit();
                return Tool::returnJson(["task_commod_id"=> $data['task_commod_id']],200,"成功");
            }
            $transaction->rollBack();
            return Tool::returnJson([],412,"发布图文任务失败");
        }catch (\Exception $e)
        {
            $transaction->rollBack();
            return Tool::returnJson([],5000,"发布图文任务异常".$e->getMessage());
        }
    }
    //发布图片任务
    public function getTaskShop($data)
    {
        if(empty($data['bus_id']))
        {
            return Tool::returnJson([],401,"商家id不能为空");
        }
        if(empty($data['task_commod_id']))
        {
            return Tool::returnJson([],402,"主任务id不能为空");
        }
        if(empty($data['safetyCode']))
        {
            return Tool::returnJson([],403,"安全不能为空");
        }
        if(md5($data['bus_id'].$data['task_commod_id']) != $data['safetyCode'])
        {
            return Tool::returnJson([],405,"安全码不正确");
        }
        $task_commod = $this->RTaskCommodAccess->getTaskCommodById($data['task_commod_id']);
        if(empty($task_commod))
        {
            return Tool::returnJson([],406,'商家发布的主任务不存在');
        }
        if($data['bus_id'] != $task_commod['bus_id'])
        {
            return Tool::returnJson([],407,'非本用户任务');
        }
        $task_shop = $this->RTaskShopImageAccess->getTaskShop($data['task_commod_id']);
        return Tool::returnJson($task_shop,200,"成功");
    }
}